package baptizeattribute.event;

import baptizeattribute.baptize.Baptize;
import baptizeattribute.dao.ItemDao;
import baptizeattribute.data.Data;
import baptizeattribute.inventory.Inv;
import baptizeattribute.pojo.Item;
import baptizeattribute.utils.Tools;
import org.bukkit.Material;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

public class Cmd implements CommandExecutor {
    private final String serverInfo = Data.config.getInfo();
    private final String cmdOpen = "open";
    private final String cmdGetItem = "getItem";
    private final String cmdSetItem = "setItem";
    private final String cmdReload = "reload";
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("xl")) {
            if (!(sender instanceof Player)) {
                sender.sendMessage("控制台无法输入该指令");
                return true;
            }
            Player p = (Player) sender;
            if (args.length == 0 && p.isOp()){
                p.sendMessage("§a======== §e洗炼系统 §a========");
                p.sendMessage("§a/xl open  §c->  §a打开洗炼GUI");
                p.sendMessage("§a/xl setItem  §c->  §a设置手中的物品为洗练道具");
                p.sendMessage("§a/xl getItem  §c->  §a给予自己一个洗练道具");
                p.sendMessage("§a/xl reload  §c->  §a重载插件");
                p.sendMessage("§a===========================");
            }
            if (args.length==1){
                //打开容器
                if (cmdOpen.equalsIgnoreCase(args[0])){
                    p.closeInventory();
                    Inv.OpenGui(p);
                }
                if (p.isOp()){
                    //给予道具
                    if (cmdGetItem.equalsIgnoreCase(args[0])){
                        if (Tools.addPlayerByItem(p, Data.item.getItem(),1)){
                            p.sendMessage(serverInfo+"§a 已送达");
                        }else {
                            p.sendMessage(serverInfo+"§c 背包不足");
                        }
                    }
                    //设置道具
                    if (cmdSetItem.equalsIgnoreCase(args[0])){
                        ItemStack itemMain = p.getInventory().getItemInMainHand();
                        if (itemMain==null || itemMain.getType() == Material.AIR){
                            p.sendMessage(serverInfo+"§c 请手持一个物品");
                            return true;
                        }
                        Item item = new Item(itemMain);
                        new ItemDao().setItem(item);
                        Data.item=item;
                        p.sendMessage(serverInfo+"§a 设置成功");
                    }
                    //重载
                    if (cmdReload.equalsIgnoreCase(args[0])){
                        Baptize.setData();
                        p.sendMessage(serverInfo+"§a 重载成功");
                    }
                }
            }
        }
        return false;
    }
}
